package com.fanco.learn_datastruct_leetcode.datastruts;

class Node {
    public Integer value;
    public Node next;

    public Node(Integer value) {
        this.value = value;
    }

    public Node(Integer value, Node next) {
        this.value = value;
        this.next = next;
    }
}

/**
 * 单链表
 */
public class SingleLinkedList {
    public Node head;

    // 从头部添加
    public void addHead(Integer data) {
        Node node = new Node(data);
        node.next = head;
        head = node;
    }

    // 从尾部添加
    public void addLast(Integer data) {
        Node node = new Node(data);
        if (head == null) {
            head = node;
            return;
        }
        Node temp = head;
        while (temp.next != null) {
            temp = temp.next;
        }
        temp.next = node;
    }

    // 获取链表长度
    public int getLength() {
        int length = 0;
        Node temp = head;
        if (temp.next != null) {
            temp = temp.next;
            length++;
        }
        return length;

    }

    // 从指定下标处添加
    public void addByIndex(int index, Integer data) {
        if (index < 0 || index > getLength()) {
            System.out.println("当前指定下标输入不正确");
            return;
        }
        if (index == 0) {
            addHead(data);
            return;
        }
        Node node = new Node(data);
        Node temp = head;
        for (int i = 0; i < index; i++) {
            temp = temp.next;
        }
        // 指定下标的下一个节点作为插入节点的下一个
        node.next = temp.next;
        temp.next = node;

    }

    // 指定下标删除
    public void deleteByIndex(int index) {
        if (index < 0 || index > getLength()) {
            System.out.println("当前指定下标输入不正确");
            return;
        }
        if (index == 0) {
            head = head.next;
            return;
        }
        Node temp = head;
        for (int i = 0; i< index;i++) {
            temp = temp.next;
        }
        temp.next = temp.next.next;
    }

    // 根据指定坐标获取
    public Node getByIndex (int index) {
        if (index < 0 || index > getLength()) {
            System.out.println("当前指定下标输入不正确");
        }
        if (index == 0) {
            return head;
        }
        Node temp = head;
        int i = 0;
        while (temp.next != null) {
            if (i == index) {
                return temp;
            }
            i++;
            temp = temp.next;
        }
        return null;
    }

    // 打印链表值
    public void printLinkValue() {
        Node temp = head;
        while (temp.next != null) {
            System.out.println(temp.value);
            temp = temp.next;
        }
    }

    // 打印指定节点后的数据
    public void printAfterNode(Node node) {
        while (node != null) {
            System.out.println(node.value);
            node = node.next;
        }
    }

    // 清空链表
    public void clearLink(){
        head = null;
    }

    // 反转链表(没看懂)
    public Node reverseLink() {
        Node prev = null;
        Node cur = head;
        while (cur != null) {
            Node nextTemp = cur.next;
            cur.next = prev;
            prev = cur;
            cur = nextTemp;
        }
        return prev;
    }

    public static void main(String[] args) {
        SingleLinkedList singleLinkedList = new SingleLinkedList();

        singleLinkedList.addLast(1);
        singleLinkedList.addLast(2);
        singleLinkedList.addLast(3);
        singleLinkedList.addLast(4);
        singleLinkedList.addLast(5);
        singleLinkedList.addLast(6);
        System.out.println("正常输出链表数据");
        singleLinkedList.printLinkValue();
        System.out.println("链表反转后输出数据");

        singleLinkedList.printAfterNode(singleLinkedList.reverseLink());
    }


}
